<template>
  <el-row>
    <!-- 头部 -->
    <el-col :span="24">
      <el-card shadow="hover">
        <el-breadcrumb :separator-icon="ArrowRightBold">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>设置中心</el-breadcrumb-item>
          <el-breadcrumb-item>参数设置</el-breadcrumb-item>
        </el-breadcrumb>
      </el-card>
    </el-col>
    <div style="padding: 5px;"></div>
    <!-- 内容区域 -->
    <el-col :span="24">
      <el-card shadow="hover">
        <div style="margin-bottom: 20px;">
          <el-alert title="参数设置" type="success" :closable="false"></el-alert>
        </div>

        <!-- 表格 -->
        <el-form label-width="210px" label-position="right" style="width: 80%;">
          <el-form-item label="短信开关" class="yxOpenBox">
            <div>
              <span>网易云:</span>
              <el-tooltip :content="formData.wydx_open == 1 ? '开启' : '关闭'" placement="top">
                <el-switch v-model="formData.wydx_open" :active-value="1" :inactive-value="0"></el-switch>
              </el-tooltip>
            </div>
            <div style="margin-right:10px;">
              <span>阿里云:</span>
              <el-tooltip :content="formData.aly_open == 1 ? '开启' : '关闭'" placement="top">
                <el-switch v-model="formData.aly_open" :active-value="1" :inactive-value="0"></el-switch>
              </el-tooltip>
            </div>
            <div style="margin-right:10px;">
              <span>短信宝:</span>
              <el-tooltip :content="formData.dxb_oepn == 1 ? '开启' : '关闭'" placement="top">
                <el-switch v-model="formData.dxb_oepn" :active-value="1" :inactive-value="0"></el-switch>
              </el-tooltip>
            </div>
            <div style="margin-right:10px;">
              <span>网易语音:</span>
              <el-tooltip :content="formData.wyyy_open == 1 ? '开启' : '关闭'" placement="top">
                <el-switch v-model="formData.wyyy_open" :active-value="1" :inactive-value="0"></el-switch>
              </el-tooltip>
            </div>
          </el-form-item>
          <el-form-item label="网易短信key">
            <el-input type="text" v-model="formData.wydx_key" @input="val => updateFormValue('wydx_key', val)"/>
          </el-form-item>
          <el-form-item label="网易短信secret">
            <el-input type="text" v-model="formData.wydx_secret" @input="val => updateFormValue('wydx_secret', val)"/>
          </el-form-item>
          <el-form-item label="网易短信模版id">
            <el-input type="text" v-model="formData.wydx_modelId"/>
          </el-form-item>
          <el-form-item label="网易语音短信模版id">
            <el-input type="text" v-model="formData.wyyydx_modelId"/>
          </el-form-item>
          <el-form-item label="网易密钥ID secretId">
            <el-input type="text" v-model="formData.wy_secretId" @input="val => updateFormValue('wy_secretId', val)"/>
          </el-form-item>
          <el-form-item label="网易密钥 secretKey">
            <el-input type="text" v-model="formData.wy_secretKey" @input="val => updateFormValue('wy_secretKey', val)"/>
          </el-form-item>
          <el-form-item label="网易业务ID businessId">
            <el-input type="text" v-model="formData.wy_businessId" @input="val => updateFormValue('wy_businessId', val)"/>
          </el-form-item>
          <el-form-item label="短信宝账号">
            <el-input type="text" v-model="formData.smsbao_user" @input="val => updateFormValue('smsbao_user', val)"/>
          </el-form-item>
          <el-form-item label="短信宝密码">
            <el-input type="text" v-model="formData.smsbao_pass" @input="val => updateFormValue('smsbao_pass', val)"/>
          </el-form-item>
          <el-form-item label="短信宝签名">
            <el-input type="text" v-model="formData.smsbao_sign"/>
          </el-form-item>

          <el-form-item label="阿里云短信AccessKey ID">
            <el-input type="text" v-model="formData.aliyun_accessKeyId" @input="val => updateFormValue('aliyun_accessKeyId', val)"/>
          </el-form-item>
          <el-form-item label="阿里云短信AccessKey Secret">
            <el-input type="text" v-model="formData.aliyun_accessKeySecret" @input="val => updateFormValue('aliyun_accessKeySecret', val)"/>
          </el-form-item>
          <el-form-item label="阿里云短信模板Code">
            <el-input type="text" v-model="formData.aliyun_templateCode"/>
          </el-form-item>
          <el-form-item label="阿里云短信签名">
            <el-input type="text" v-model="formData.aliyun_signName"/>
          </el-form-item>

          <el-form-item label="系统域名">
            <el-input type="text" v-model="formData.app_host"/>
          </el-form-item>

          <el-form-item label="加拿大28开奖接口">
            <el-input type="text" v-model="formData.app_issue_url"/>
          </el-form-item>
          <el-form-item label="香港六合彩开奖接口">
            <el-input type="text" v-model="formData.app_issue_url_xglhc"/>
          </el-form-item>
          <el-form-item label="新澳门六合彩开奖接口">
            <el-input type="text" v-model="formData.app_issue_url_xamlhc"/>
          </el-form-item>
          <el-form-item label="老澳门六合彩开奖接口">
            <el-input type="text" v-model="formData.app_issue_url_lamlhc"/>
          </el-form-item>

          <el-form-item label="客服链接域名">
            <el-input type="text" v-model="formData.app_imurl"/>
          </el-form-item>

<!--          <el-form-item label="微信跳转返回域名">-->
<!--            <el-input type="text" v-model="formData.app_app_url"/>-->
<!--          </el-form-item>-->
          <el-form-item label="微信公众号绑定域名">
            <el-input type="text" v-model="formData.app_weixing_url"/>
          </el-form-item>
<!--          <el-form-item label="微信公众号appid">-->
<!--            <el-input type="text" v-model="formData.app_weixing_appid"   @input="val => updateFormValue('app_weixing_appid', val)" placeholder=""/>-->
<!--          </el-form-item>-->
<!--          <el-form-item label="微信公众号app_secret">-->
<!--            <el-input type="text" v-model="formData.app_weixing_secret"  @input="val => updateFormValue('app_weixing_secret', val)" placeholder=""/>-->
<!--          </el-form-item>-->

          <el-form-item label="自开彩28开奖延时(秒)">
            <el-input type="number" v-model="formData.app_aozhou_time"/>
          </el-form-item>
          <el-form-item label="香港/澳门六合彩每次下注数量">
            <el-input type="number" v-model="formData.lhc_xz_number"/>
          </el-form-item>
          <el-form-item label="香港/澳门六合彩每期特码数量">
            <el-input type="number" v-model="formData.lhc_tm_number"/>
          </el-form-item>
          <el-form-item label="香港/澳门六合彩每期特肖数量">
            <el-input type="number" v-model="formData.lhc_tx_number"/>
          </el-form-item>
          <el-form-item label="香港/澳门六合彩每期平特一肖数量">
            <el-input type="number" v-model="formData.lhc_ptyx_number"/>
          </el-form-item>
<!--          <el-form-item label="加拿大28开奖延时(秒)">-->
<!--            <el-input type="number" v-model="formData.app_jianada_time"/>-->
<!--          </el-form-item>-->
<!--          <el-form-item label="香港28开奖延时(秒)">-->
<!--            <el-input type="number" v-model="formData.app_aomenjisu_time"/>-->
<!--          </el-form-item>-->
<!--          <el-form-item label="澳门28开奖延时(秒)">-->
<!--            <el-input type="number" v-model="formData.app_xinjiapo_time"/>-->
<!--          </el-form-item>-->
<!--          <el-form-item label="澳门六合彩开奖延时(秒)">-->
<!--            <el-input type="number" v-model="formData.app_aomen_time"/>-->
<!--          </el-form-item>-->
<!--          <el-form-item label="香港六合彩开奖延时(秒)">-->
<!--            <el-input type="number" v-model="formData.app_xianggang_time"/>-->
<!--          </el-form-item>-->
          <el-form-item>
            <el-button type="primary" @click="submitForm">保存设置</el-button>
          </el-form-item>
        </el-form>

        <!-- 分页 -->
      </el-card>
    </el-col>
  </el-row>
</template>

<script setup>

import {ref, reactive, onMounted, inject, getCurrentInstance} from 'vue'
import {success, error} from '@/common/tool/message.js'
import {request_post} from '@/common/axios/request.js'
import {ArrowRightBold} from '@element-plus/icons-vue'

const {proxy} = getCurrentInstance();
const imgData = reactive({list: []})

// 实际存储的原始值
const originalData = reactive({
  wydx_open: '0',
  aly_open: '0',
  dxb_oepn: '0',
  wyyy_open: '0',
  wydx_key: '',
  wydx_secret: '',
  wydx_modelId:'',
  wyyydx_modelId:'',
  wy_secretId: '',
  wy_secretKey: '',
  wy_businessId: '',
  smsbao_user: '',
  smsbao_pass: '',
  smsbao_sign: '',

  aliyun_accessKeyId: '',
  aliyun_accessKeySecret: '',
  aliyun_templateCode: '',
  aliyun_signName: '',

  app_host: '',
  app_issue_url: '',
  app_issue_url_xglhc: '',
  app_issue_url_xamlhc: '',
  app_issue_url_lamlhc: '',
  app_imurl: '',

  app_app_url: '',
  app_weixing_url: null,
  app_weixing_appid: null,
  app_weixing_secret: null,

  app_aomen_time: null,
  app_aozhou_time: null,
  app_jianada_time: null,
  app_xinjiapo_time: null,
  app_aomenjisu_time: null,
  app_xianggang_time: null,
  lhc_xz_number: null,
  lhc_tx_number: null,
  lhc_ptyx_number: null,
  lhc_tm_number:null,
})

// 用于显示的响应式对象
const formData = reactive({ ...originalData })

onMounted(() => {
  adminConfigIndex()
})


function submitForm() {
  adminConfigCreate()
}


/**
 * 对敏感信息进行脱敏处理
 * @param {string} value - 原始值
 * @returns {string} 脱敏后的值，格式为"前2位+****+后2位"
 */
function maskSensitiveInfo(value) {
  return value ? value.substring(0, 4) + '**********' + value.slice(-4) : ''
}

// 检查并更新表单值，保留原始值
function updateFormValue(key, value) {
  originalData[key] = value

}

function adminConfigIndex() {
  request_post('/admin_config_index').then(res => {
    if (res.data.code == 200) {
      // 更新原始值和显示值
      for (const key in res.data.data) {
        if (key in originalData) {
          originalData[key] = res.data.data[key]
          // 敏感字段显示脱敏值，非敏感字段直接显示
          const sensitiveFields = [
            'wydx_key', 'wydx_secret',
            'wy_secretId', 'wy_secretKey',
            'smsbao_user', 'smsbao_pass',
            'aliyun_accessKeyId', 'aliyun_accessKeySecret',
              'wy_businessId','app_weixing_appid','app_weixing_secret'
          ]
          formData[key] = sensitiveFields.includes(key)
            ? maskSensitiveInfo(res.data.data[key])
            : res.data.data[key]
        }
      }


    } else {
      return error(res.data.msg)
    }
  })
}

// 添加配置
function adminConfigCreate() {


  originalData.aly_open = formData.aly_open
  originalData.wydx_open = formData.wydx_open
  originalData.dxb_oepn = formData.dxb_oepn
  originalData.wyyy_open = formData.wyyy_open
  originalData.wydx_modelId = formData.wydx_modelId
  originalData.wyyydx_modelId = formData.wyyydx_modelId
  originalData.smsbao_sign = formData.smsbao_sign
  originalData.aliyun_signName = formData.aliyun_signName
  originalData.app_host = formData.app_host
  originalData.app_issue_url = formData.app_issue_url
  originalData.app_issue_url_xglhc = formData.app_issue_url_xglhc
  originalData.app_issue_url_xamlhc = formData.app_issue_url_xamlhc
  originalData.app_issue_url_lamlhc = formData.app_issue_url_lamlhc
  originalData.app_imurl = formData.app_imurl
  originalData.app_app_url = formData.app_app_url
  originalData.app_aomen_time = formData.app_aomen_time
  originalData.app_aozhou_time = formData.app_aozhou_time
  originalData.app_jianada_time = formData.app_jianada_time
  originalData.app_xianggang_time = formData.app_xianggang_time
  originalData.app_xinjiapo_time = formData.app_xinjiapo_time
  originalData.app_aomenjisu_time = formData.app_aomenjisu_time
  originalData.app_weixing_url = formData.app_weixing_url
  originalData.app_weixing_appid = formData.app_weixing_appid
  originalData.app_weixing_secret = formData.app_weixing_secret
  originalData.app_weixing_url = formData.app_weixing_url
  originalData.lhc_xz_number = formData.lhc_xz_number
  originalData.lhc_tx_number = formData.lhc_tx_number
  originalData.lhc_ptyx_number = formData.lhc_ptyx_number
  originalData.lhc_tm_number = formData.lhc_tm_number
  request_post('/admin_config_create', originalData).then(res => {
    location.reload()
    if (res.data.code == 200) {

      return success(res.data.msg)

    } else {
      return error(res.data.msg)
    }

  })
}


function deleteUrl(index) {
  imgData.list.splice(index, 1)
}

const imageModel = inject('showImageModel')
const imageUrlArray = ref(inject('imageUrlArray'))

// 图片上传
function ImageUpload() {
  imageModel()
  imgData.list = imageUrlArray
}

</script>

<style></style>
